$(document).ready(function() {
	$("#title").hide();//隐藏表格头
	$("#self-menu").click(function() {
		$("#title").hide();
		$("#food").empty();
	});
	
	$("#reset").click(function(){
		$("#threeDine tr").each(function(i,row){
			if(i>1){
				for(var j=2;j<row.cells.length;j++){
					row.cells[j].innerHTML="";
				}
				
			}
		})
	});
});

//一次提交请求5种不同的食物,作为一餐分量计算营养,可以把三餐相加
function formRecipe(){
var map={};
var selects=$("form#eatForm").find("select");
var text=$("form#eatForm").find(":text");
for(var i=0;i<text.length;i++){ 
	var temp=selects.eq(i).val();
	var num=text.eq(i).val();
	if(/(^(\d+)$|^(\d+)\.(\d+)$)/.test(num))
	map[temp]=num;
	else{
		alert("数据格式错误!");
		return;
	}
		
}
$.ajax({
    url: "http://localhost:8080/SSMFrame/jsp/recipe.do",
   type: "POST",
    contentType : 'application/json;charset=utf-8', //设置请求头信息
    dataType:"json", 
    data: JSON.stringify(map),         //将Json对象序列化成Json字符串，toJSON()需要引用jquery.json.min.js
    success: function(datas){
        $.each(datas,function(key,field){
            	var temp=$("table#threeDine").find("."+key);
            	for(var j=0;j<temp.length;j++){
            		if(temp.eq(j).text())
            			continue;
            		var str=Number(datas[key]/100).toFixed(2);
            		temp.eq(j).text(str);
            		break;
            	}
            });
        addTotal();
        }
    ,
   
    error: function(res){
        alert(res.responseText);
    }
});
}

//三餐相加
function addTotal(){
	var objTable=document.getElementById("threeDine");
	   for(var i=2;i<14;i++)
        {
           var num=0;
		   for(var j=2;j<5;j++)
			   num+=Number(objTable.rows[j].cells[i].innerHTML);
           objTable.rows[5].cells[i].innerText=num;
           }
}

function checkRecipe(forms) {//验证form的数据格式
	var flag = false;
	var var1 = forms.elements[1].value;
	var var2 = forms.elements[3].value;
	var var3 = forms.elements[5].value;
	var var4 = forms.elements[7].value;
	var var5 = forms.elements[9].value;
	if (/(^(\d+)$|^(\d+)\.(\d+)$)/.test(var1)
			&& /(^(\d+)$|^(\d+)\.(\d+)$)/.test(var2)
			&& /(^(\d+)$|^(\d+)\.(\d+)$)/.test(var3)
			&& /(^(\d+)$|^(\d+)\.(\d+)$)/.test(var4)
			&& /(^(\d+)$|^(\d+)\.(\d+)$)/.test(var5))
		flag = true;
	else
		alert("输入数据格式不对!");
	return flag;
}





function searchFood() {
	$("#title").show();//显示表头
	var key = $("#keyWord").val();
	$
			.get(
					"http://localhost:8080/SSMFrame/jsp/searchFood.do?key=" + key,
					function(result) {
						if("请登录"==result)
						alert(result);
						$
								.each(
										result,
										function(i, field) {
											$("#food")
													.prepend(
															"<tr><td><span id='id'></span></td><td><span id='name'></span></td>"
																	+ "<td><span id='energy'></span></td><td><span id='protein'></span></td>"
																	+ "<td><span id='fat'></span></td><td><span id='sugar'></span></td><td><span id='ca'></span></td>"
																	+ "<td><span id='fe'></span></td><td><span id='va'></span></td><td><span id='vb1'></span></td>"
																	+ "<td><span id='vb2'></span></td><td><span id='vc'></span></td><td><span id='vd'></span></td>"
																	+ "<td><span id='ve'></span></td><td><input id='quality' value='0'></td></tr>");
											$.each(field, function(j, num) {
												$("#food").find("tr").eq(0)
														.find("#" + j)
														.text(num);
											})
										});
					});
}

//表格计算,每列求和
function calculate() {
	var counts = { //作为合计值的模版
		0 : "",
		1 : "合计",
		2 : 0,
		3 : 0, 
		4 : 0,
		5 : 0,
		6 : 0,
		7 : 0,
		8 : 0,
		9 : 0,
		10 : 0,
		11 : 0,
		12 : 0,
		13 : 0
	};
	var foodtr = $("#food").children().not("#counttr");//取出食物信息所在的行
	var foodQuality = $("#food :input");//所有用户输入的质量数
	for(var i=0;i<foodtr.length;i++) {
		
		var num = foodQuality.eq(i).val();//输入的质量数
		if(num!=0)
			foodtr.eq(i).css("color","blue");
		if (!/(^(\d+)$|^(\d+)\.(\d+)$)/.test(num)) {
			alert("请输入数字!")
			foodtr.eq(i).css("color","red");
			return;
		}
		var foodnum = foodtr.eq(i).find("span");//某一行的信息
		for (var j = 2; j < foodnum.length; j++) {//依次加上某一行的数值
			var tempnum = foodnum.eq(j).text() * num/100;
			//alert(counts[i]);
			counts[j] += tempnum;
		}
	}
	if ($("#counttr").length == 0 && foodtr.length !== 0) {
		$("#food")
				.append(
						"<tr id='counttr'><td><span></span></td><td><span></span></td><td><span></span></td>"
								+ "<td><span></span></td><td><span></span></td><td><span></span></td><td><span></span></td><td><span></span></td><td><span></span></td>"
								+ "<td><span></span></td><td><span></span></td><td><span></span></td><td><span></span></td><td><span></span></td><td></td></tr>");
	}
	var counttr = $("#counttr").find("span");
	for (var i = 0; i < counttr.length; i++) {
		var temp = counts[i];
		counttr.eq(i).text(temp);
	}
}

//保存食谱
function saveRecipe(){
	var str={};
	$("div#setRecipe").find("div").each(function(){
		var key=$(this).find(":text").eq(0).val();
		var temp=$(this).find(":text").eq(1).val();
		if(!/(^(\d+)$|^(\d+)\.(\d+)$)/.test(temp)&&temp!=""){
			alert("请输入数字!");
			return;
		}
		str[key]=temp;
	});
	alert(JSON.stringify(str));
	var map={"name":$("#setRecipe").find(":text").eq(0).val(),
			"description":$("#setRecipe").find("textarea").eq(0).text(),
			"content":JSON.stringify(str)
	};
	
	$.post("http://localhost:8080/SSMFrame/jsp/saveRecipe.do",map,function(data,state){
		
	})
}

//添加食物
function addFood(){
	$("#setRecipe").append("<div>食物:<input>&emsp;质量(g):<input></div>");
}
